#include "SEL.h"
#include "SEL_painter.h"
#include "SEL_resources.h"

using namespace SEL;

class QuitHandler : public EventHandler
{
	virtual bool handleQuitEvent ()
	{
		exit (0);
		return true; // make mr. compiler happy
	}
	virtual bool handleKeyPressEvent (SDL::keysym & keysym)
	{
		exit (0);
    		return true;
	}
};

int main ()
{
  try
  {
	debug::init (true);

	atexit (quit);

	resourcesInit ();

	ResourceManager manager ("fonts.rs");

//	ResourceManager manager("fonts.dat",true);
//	manager.createDataFile ("fonts.dat");

	// load all resources
	manager.loadAll ();
	
	// The fonts are owned by the resource manager.
	Font *font1 = getFont (manager.get ("fonts/font1"));
	Font *font2 = getFont (manager.get ("fonts/font2"));

	init (INIT_VIDEO);	
	Display *display = new Display (640, 480, 16, 
					SDL::SWSURFACE|SDL::FULLSCREEN);

	font1->setTargetSurface (display);
	font2->setTargetSurface (display);
	
	// draw black to white horizontal gradients
	for (int i = 0; i < display->w (); i+= 1)
	{
		display->fillRect (Rect (i, 0, 1, display->h()), i%255, 
				   i%255, i%255);
	}

	font1->print (10, 10, "This is alpha blended\nantialiased font\n");
	font1->print (10, 300, "These fonts are little difficult to read,\n"
			       "but they are visible on any kind of background...");

	font2->print (300, 10, "This is key-colored\nfont. It's not so smooth,\n"
			       "but it's faster.");
	font2->print (10, 370, "These fonts are little difficult to read,\n"
			       "but they are visible on any kind of background...");


	display->flip ();
	
	manager.freeAll ();
	
	QuitHandler quit_handler;
	while (1)
	{
		Event::handleEvents (quit_handler);
	}
  }
  catch (Error error)
  {
  	std::cout << "Exception caught: " << error.str () << std::endl;
  }
	return 0;
}
